import com.alibaba.fastjson.JSON;
import com.aliyun.odps.udf.UDF;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.HashMap;
import java.util.Map;

public class SpliderBabyhavenGoods extends UDF {
   /* public static void main(String[] args) {
        HttpClientAPI client = new HttpClientAPI();
        try {
            HttpResult httpResult = client.doGet("https://cn.babyhaven.com/product/1061081.html");
            SpliderBabyhavenGoods target = new SpliderBabyhavenGoods();
            target.evaluate(httpResult.getBody());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }*/

    public String evaluate(String html) {
        if (StringUtils.isNotBlank(html)) {
            Map<String, String> resultMap = new HashMap<>();
            Document document = Jsoup.parse(html);
            //title
            Elements titleClass = document.getElementsByClass("product-name");
            if (null != titleClass && titleClass.size() > 0) {
                resultMap.put("title", titleClass.text());
            }
            //price
            Element idEle = document.getElementById("end-price");
            if (null != idEle) {
                resultMap.put("price", "$" + idEle.text());
            }
            //image
            Elements imgClass = document.getElementsByClass("showcase");
            if (null != imgClass && imgClass.size() > 0) {
                String attr = imgClass.get(0).attr("src");
                resultMap.put("image", attr);
            }

            //description
            Elements descClass = document.getElementsByClass("pds-product-description");
            if (null != descClass && descClass.size() > 0) {
                Elements elementsByClass = descClass.get(0).getElementsByClass("d-list");
                if (null != elementsByClass && elementsByClass.size() > 0) {
                    for (Element ele : elementsByClass) {
                        String text = ele.text();
                        if (text.contains("：")) {
                            String[] split = text.split("：");
                            if (split.length == 2) {
                                resultMap.put(split[0].trim().replaceAll("[^0-9a-zA-Z\\u4e00-\\u9fa5]",""), split[1]);
                            }
                        }
                    }
                }
            }
            //不同页面
            if (StringUtils.isBlank(resultMap.get("description"))) {
                Elements elementsByClass = descClass.get(0).getElementsByClass("tpl-item");
                if (null != elementsByClass && elementsByClass.size() > 0) {
                    StringBuffer sb = new StringBuffer();
                    for (Element ele : elementsByClass) {
                        String text = ele.text().replaceAll("：|:",":");;
                        if (text.contains(":")) {
                            String[] split = text.split(":");
                            if (split.length == 2) {
                                resultMap.put(split[0].trim().replaceAll("[^0-9a-zA-Z\\u4e00-\\u9fa5]",""), split[1]);
                            }
                        }
                    }
                }
            }

            if (StringUtils.isBlank(resultMap.get("description"))) {
                String s = descClass.toString();
                String[] split = s.split("<br>");
                //Elements brEles = descClass.get(0).getElementsByTag("br");
                for (String str : split) {
                    if(StringUtils.isNotBlank(str)){
                        String[] splitStr = str.split("\n");
                        for (String br :splitStr){
                            br = br.replaceAll("：|:",":");
                            if (StringUtils.isNotBlank(br) && br.contains("丨")) {
                                String[] split1 = br.split("丨");
                                if (split1.length == 2) {
                                    resultMap.put(split1[0].trim().replaceAll("[^0-9a-zA-Z\\u4e00-\\u9fa5]",""), split1[1].replace("\n", ""));
                                }
                            }else if (StringUtils.isNotBlank(br) && br.contains(":")) {
                                String[] split1 = br.split(":");
                                if (split1.length == 2) {
                                    resultMap.put(split1[0].trim().replaceAll("[^0-9a-zA-Z\\u4e00-\\u9fa5]",""), split1[1].replace("\n", ""));
                                }
                            }
                        }
                    }
                }
            }

            if (StringUtils.isBlank(resultMap.get("description"))) {
                resultMap.put("description", descClass.text());
            }
            String result = JSON.toJSONString(resultMap).replace("　", "");
            System.out.println(result);
            return result;
        }
        return null;
    }
}
